/* -+- file  ./kf2/GUI/AccountDialog.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Core;
using Habazie.kf2.Net;

namespace Habazie.kf2.GUI
{
/*	private class Bound : Attribute
	{
		public string Field;
		public Bound (string f) : base ()
		{
			Field = f;
		}
	}*/
	
	
	public class AccountDialog : Gtk.Dialog
	{
		private Account m_Account;
		
		protected Gtk.Entry m_AccountName;
		protected Gtk.Entry m_Username;
		protected Gtk.Entry m_Server;
		protected Gtk.Entry m_Password;
		protected Gtk.CheckButton m_SavePassword;
		protected Gtk.Entry m_Resource;
		protected Gtk.Entry m_NetworkHost;
		protected Gtk.SpinButton m_Priority;
		protected Gtk.SpinButton m_Port;
		protected Gtk.CheckButton m_AutoReconnect;
		protected Gtk.CheckButton m_AllowPlaintext;
		protected Gtk.CheckButton m_TLS;
		protected Gtk.CheckButton m_SSL;

		
		public AccountDialog(Account acc) : base ()
		{
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.AccountDialog));
			m_Account = acc;
			
			Update ();
			
			m_AccountName.Changed += Changed;
			m_Username.Changed += Changed;
			m_Server.Changed += Changed;
			m_Password.Changed += Changed;
			m_Resource.Changed += Changed;
			m_NetworkHost.Changed += Changed;
			m_Priority.ValueChanged += ValueChanged;
			m_Port.ValueChanged += ValueChanged;
			m_SavePassword.Toggled += Toggled;
			m_AutoReconnect.Toggled += Toggled;
			m_AllowPlaintext.Toggled += Toggled;
			m_TLS.Toggled += Toggled;
			m_SSL.Toggled += Toggled;
			//.Changed += Changed;
		}
		
		private void Update ()
		{
			if (m_Account == null)
				return;
				
			m_AccountName.Text = m_Account.Name;
			m_Username.Text = m_Account.User;
			m_Server.Text = m_Account.Server;
			m_Password.Text = m_Account.Password;
			m_SavePassword.Active = m_Account.SavePassword;
			
			m_Resource.Text = m_Account.Resource;
			m_NetworkHost.Text = m_Account.NetworkHost;
			m_Priority.Value = m_Account.Priority;
			m_Port.Value = m_Account.Port;
			
			m_AutoReconnect.Active = m_Account.AutoReconnect >= 0;
			m_AllowPlaintext.Active = m_Account.PlaintextAuth;
			m_TLS.Active = m_Account.AutoStartTLS;
			m_SSL.Active = m_Account.SSL;
		}

		///<summary>Fill this form</summary>
		protected virtual void OnCloseClicked(object sender, System.EventArgs e)
		{
			m_Account.FireChanged ();
			Destroy ();
		}
		
		
		///<summary>Some entry contents have changed</summary>
		protected virtual void Changed (object o, EventArgs args)
		{
			string val = ((Gtk.Entry) o).Text;
			Gtk.Entry e = o as Gtk.Entry;
			switch (e.Name) {
				case "m_AccountName":
					m_Account.Name = val;
					break;
				case "m_Username":
					m_Account.User = val;
					break;
				case "m_Server":
					m_Account.Server = val;
					break;
				case "m_Password":
					m_Account.Password = val;
					break;
				case "m_NetworkHost":
					m_Account.NetworkHost = val;
					break;
				default:
					Util.Debug ("GUI", "Unknown field: "+e.Name);
					break;
			}
		}
		
		protected virtual void ValueChanged (object o, EventArgs args)
		{
			Gtk.SpinButton s = o as Gtk.SpinButton;
			int val = s.ValueAsInt;
			switch (s.Name) {
				case "m_Priority":
					m_Account.Priority = val; break;
				case "m_Port":
					m_Account.Port = val; break;
			}
		}
		
		protected virtual void Toggled (object o, EventArgs args)
		{
			Gtk.ToggleButton t = o as Gtk.ToggleButton;
			bool val = t.Active;
			switch (t.Name) {
				case "m_SavePassword":
					m_Account.SavePassword = val; break;
				case "m_AutoReconnect":
					m_Account.AutoReconnect = val?30:-1; break;
				case "m_AllowPlaintext":
					m_Account.PlaintextAuth = val; break;
				case "m_TLS":
					m_Account.AutoStartTLS = val; break;
				case "m_SSL":
					m_Account.SSL = val; break;
			}
		}
	}
}
